﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Filters;
using VisionCloud.Service.Sys;

namespace VisionCloud.Api.Controllers.Sys
{
    /// <summary>
    /// 组织架构
    /// </summary>
    [SystemModule("组织架构")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiVersion("1.0")]
    [Route("api/sys/[controller]/[Action]")]
    [ApiExplorerSettings(GroupName = "组织架构")]
    public class SysOrgController : ApiBaseController
    {
        /// <summary>
        /// 组织结构服务
        /// </summary>
        public SysOrgService sysOrgService { get; set; }

        /// <summary>
        /// 创建组织基本内容
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Create(SysOrgDto model) => await TryCatchAsync(sysOrgService.CreatAsync(model));

        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpPost]
        public async Task<ApiResult> GetPage(SysOrgPageDto model) => await TryCatchAsync(sysOrgService.GetPage(model));

        /// <summary>
        /// 获取组织架构树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetOrgTree() => await TryCatchAsync(sysOrgService.GetOrgTree());

        ///// <summary>
        ///// 获取子项
        ///// </summary>
        ///// <param name="superId"></param>
        ///// <returns></returns>
        //[HttpGet("{superId}")]
        //public async Task<ApiResult> GetSubOrgs(String superId) => await TryCatchAsync(sysOrgService.GetSubOrgs(superId));

        /// <summary>
        /// 获取下一级别组织架构树
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetSubOrgs(SysOrgDto dto) => await TryCatchAsync(sysOrgService.GetSubOrgs(dto.SuperId));

        /// <summary>
        /// 分页获取人员
        /// </summary>
        /// <param name="Id">组织ID</param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        public async Task<ApiResult> SubUsers(String Id) => await TryCatchAsync(sysOrgService.GetSubUsers(Id));

        /// <summary>
        /// 添加成员到组织
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddUsers(SysOrgOPDto model) => await TryCatchAsync(sysOrgService.AddUsers(model));

        /// <summary>
        /// 添加成员到组织
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> RemoveUsers(SysOrgOPDto model) => await TryCatchAsync(sysOrgService.RemoveUser(model));

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Del(SysOrgDto model) => await TryCatchAsync(sysOrgService.DelOrg(model));
    }
}